import {Component, OnInit, ElementRef} from '@angular/core';
import {FormatTextUtils, HighlightUtils, SqlToModelConfig} from 'great-jsutils';
import {HighlightJsService} from 'angular2-highlight-js';

@Component({
  selector: 'app-tool03-sql',
  templateUrl: './tool03-sql.component.html',
  styleUrls: ['./tool03-sql.component.less']
})
export class Tool03SqlComponent implements OnInit {
  queryModel={
    dbType:'mysql',
    codeAnnotationPosition:'top',
    jpaAnnotation:'false'
  }
  sqlStr = `
   id                   bigint not null default 0  comment '主键',
   project_id           bigint not null  comment '项目ID',
   project_code         varchar(36) not null default ' '  comment '项目编码',
  `;
  oracleSql = `
    id NUMBER(4) PRIMARY KEY,
    project_id VARCHAR2(10),
    JOB VARCHAR2(9),
    project_code NUMBER(4),   
  `;
  html;

  constructor(private el: ElementRef, private highlightJsService: HighlightJsService) {
  }

  ngOnInit() {
    this.output();
  }

  output() {
    const config = SqlToModelConfig.transformByStr(this.queryModel);
    this.html = '<div>' + FormatTextUtils.sqlToJsModel(this.sqlStr, config).join('</div><div>') + '</div>';
    this.hljsDemo();
  }

  outputJavaEntity() {
    const config = SqlToModelConfig.transformByStr(this.queryModel);
    this.html = '<div>' + FormatTextUtils.sqlToJavaEntity(this.sqlStr, config).join('</div><div>') + '</div>';
    this.hljsDemo();
  }

  hljsDemo() {
    setTimeout(() => {
      this.highlightJsService.highlight(this.el.nativeElement.querySelector('.js'));
    }, 300);
  }

}
